<?php
/**
 * Created by PhpStorm.
 * User: 28944
 * Date: 2018/5/25
 * Time: 15:23
 */

namespace app\index\controller;
use think\Controller;
use think\Cache;
use think\Validate;
use think\Queue;
use think\Db;
use think\captcha\Captcha;

class Login extends Common
{

    public function index(){

        if(request()->isAjax()){
            if(!captcha_check(input('validate'))){
                return json(['msg'=>'验证码有误！']);
            }else{
                $data = input('post.');
                $newData = [];
                foreach ($data as $k=>$y){
                    if($k == "username" || $k == "password"){
                       $newData = [
                           'u_user'=>$data['username'],
                           'u_pwd'=>$data['password'],
                           'u_uptime'=>DateTime(time())
                       ];
                    }
                }
                $validate = validate('login');

                if(!$validate->check($newData)){
                    return json(['msg'=>$this->error($validate->getError())]);

                }
                if(db('user')->where('u_user = "'. $newData['u_user'] .'" and u_status = 1')->save(['u_uptime'=>DateTime(time())])){
                    return json(['msg'=>'登录成功','url'=>url('/index')]);
                }
                return json(['msg'=>'帐号或密码有误！']);

            }
        }

        return view();

    }

    public function reg(){

        return view();
    }

    public function off(){

        return view();
    }

    public function code()
    {
        $config =    [
            // 验证码字体大小
            'fontSize'    =>    16,
            // 验证码位数
            'length'      =>    5,
            'fontttf'      =>   "4.ttf",
            // 关闭验证码杂点
            'useNoise'    =>    false,
            'useCurve'    =>    false,
            'useImgBg'    =>    false,
        ];
        $captcha = new Captcha($config);
        return $captcha->entry();
    }
}